package com.cinemahorairerapido.controller;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;

import com.cinemahorairerapido.bean.Cinema;

public class FavorisController {

	private static FavorisController favorisController = null;

	private final List<Cinema> favoriteCinemas = new ArrayList<Cinema>();

	private Context mBase = null;

	private Integer currentId = 0;

	/**
	 * Singleton
	 */
	public static FavorisController getInstance(Context base) {
		if (favorisController == null) {
			favorisController = new FavorisController(base);
			favorisController.loadFavoris();
		}

		favorisController.mBase = base;

		return favorisController;
	}

	protected FavorisController(Context base) {
		this.mBase = base;
	}

	public static String FAVORI_FILE = "FAVORI_FILE";

	protected void loadFavoris() {

		try {

			FileInputStream fIn = mBase.openFileInput(FavorisController.FAVORI_FILE);

			// int ch;
			// StringBuffer strContent = new StringBuffer("");
			//
			// while ((ch = fIn.read()) != -1) {
			// strContent.append((char) ch);
			// }
			//
			// // Transform the chars to a String
			// String readString = strContent.toString();

			BufferedReader br = new BufferedReader(new InputStreamReader(fIn));
			String strLine = "";

			// read comma separated file line by line
			while ((strLine = br.readLine()) != null) {
				// process the line, stuff to List
				String[] values = strLine.split(";");

				if (values.length == 5) {

					Cinema favoriteCinema = new Cinema();

					// add favorite cinema
					favoriteCinema.setName(values[0]);
					favoriteCinema.setAllocineCode(values[1]);
					favoriteCinema.setAddress(values[2]);
					favoriteCinema.setPostalCode(values[3]);
					favoriteCinema.setCity(values[4]);
					favoriteCinema.setId(currentId++);

					favoriteCinemas.add(favoriteCinema);
				}
			}

			fIn.read();
			fIn.close();

		} catch (IOException e) {
			Log.e("FavorisController", e.getLocalizedMessage());
		}
	}

	/**
	 * Add / Remove favori
	 * 
	 * @param stationIndex
	 * @param isFavori
	 */
	public void updateFavoris(Cinema cinema, boolean isFavori) {

		boolean favoritesModified = false;

		// find if in the list
		Cinema existingFavoriteCinema = null;

		for (Cinema f : favoriteCinemas) {
			if (f.getAllocineCode().equals(cinema.getAllocineCode())) {
				existingFavoriteCinema = f;
			}
		}

		if (!isFavori && existingFavoriteCinema != null) {
			favoriteCinemas.remove(existingFavoriteCinema);
			favoritesModified = true;
		} else if (isFavori && existingFavoriteCinema == null) {
			// create a copy
			Cinema newFavorite = new Cinema();

			newFavorite.setName(cinema.getName());
			newFavorite.setAllocineCode(cinema.getAllocineCode());
			newFavorite.setAddress(cinema.getAddress());
			newFavorite.setCity(cinema.getCity());
			newFavorite.setPostalCode(cinema.getPostalCode());
			newFavorite.setId(this.currentId++);

			favoriteCinemas.add(newFavorite);
			favoritesModified = true;
		}

		if (favoritesModified) {

			String string = "";

			for (Cinema c : favoriteCinemas) {
				string += c.getName() + ";" + c.getAllocineCode() + ";" + c.getAddress() + ";" + c.getPostalCode() + ";" + c.getCity() + "\n";
			}

			try {

				FileOutputStream fos = mBase.openFileOutput(FavorisController.FAVORI_FILE, Context.MODE_PRIVATE);

				fos.write(string.getBytes());
				fos.close();

			} catch (IOException e) {
				Log.e("FavorisController", e.getLocalizedMessage());
			}
		}

	}

	public List<Cinema> getFavoriteCinemas() {
		return favoriteCinemas;
	}

	/**
	 * 
	 * @param cinema
	 * @return true if the cinema is favorite
	 */
	public boolean isFavorite(Cinema cinema) {

		for (Cinema c : favoriteCinemas) {
			if (cinema.getAllocineCode().equals(c.getAllocineCode())) {
				return true;
			}
		}

		return false;
	}

}
